package com.panda.pojo;

import lombok.Getter;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Getter
public class Document {


    //解析出来的文件
    private Files files;

    //解析出来的名称
    private String name;

    //解析出来的属性字符串
    private String attrs;

    //原始的属性字符串
    private String rawAttrs;

    //依赖的字符串
    private Set<String> deps;

    private String as;

    private String namespace;

    //体
    private String body;

    int pow = Integer.MIN_VALUE;

    public void setPow(int pow) {
        this.pow = pow;
    }

    public Document(String filesS, String name, String attrs, String rawAttrs, String deps, String as, String namespace, String body) {

        this.files = Files.create(filesS);

        if (deps != null) {

            this.deps = new HashSet<>();

            String[] split = deps.split(",");

            for (String dss : split) {

                String sss = dss.trim();

                if (!"".equals(sss)) {
                    this.deps.add(sss);
                }

            }

        }


        this.name = name;
        this.attrs = attrs;
        this.rawAttrs = rawAttrs;
        this.as = as;
        this.namespace = namespace;
        this.body = body;
    }

    @Getter
    public static class Files {


        private Set<String> js = new HashSet<>();
        private Set<String> css = new HashSet<>();

        private Files() {
        }


        private static Pattern jsp = Pattern.compile("js[\n ]*:[\n ]*((\\[[^\\[\\]]+])|([^,}]+)),?");
        private static Pattern cssp = Pattern.compile("css[\n ]*:[\n ]*((\\[[^\\[\\]]+])|([^,}]+)),?");
        private static Pattern groupp = Pattern.compile("group[\n ]*:[\n ]*((\\[[^\\[\\]'\"]+])|([^,}'\"]+)),?");

        public static Files create(String text) {

            if (text == null || "".equals(text)) {

                return null;

            }

            Files files = new Files();

            Matcher matcher = jsp.matcher(text);

            while (matcher.find()) {

                String r = matcher.group(1).trim();

                if (r.startsWith("[") && r.endsWith("]")) {

                    r = r.substring(1, r.length() - 1);

                    String[] split = r.split(",");

                    files.js.addAll(Arrays.asList(split));

                } else {
                    files.js.add(r);
                }

            }

            matcher = cssp.matcher(text);

            while (matcher.find()) {

                String r = matcher.group(1).trim();

                if (r.startsWith("[") && r.endsWith("]")) {

                    r = r.substring(1, r.length() - 1);

                    String[] split = r.split(",");

                    files.css.addAll(Arrays.asList(split));

                } else {
                    files.css.add(r);
                }

            }


            matcher = groupp.matcher(text);

            while (matcher.find()) {

                String r = matcher.group(1).trim();

                if (r.startsWith("[") && r.endsWith("]")) {

                    r = r.substring(1, r.length() - 1);

                    String[] split = r.split(",");

                    for (String s : split) {

                        files.js.add(s);
                        files.css.add(s);
                    }

                } else {
                    files.js.add(r);
                    files.css.add(r);
                }

            }


            return files;
        }


    }

}

